<?
/*
CREATE TABLE `user_reviews` (
  `sno` int(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(30) DEFAULT NULL,
  `image` text,
  `review` text,
  `rating` varchar(10) DEFAULT NULL,
  `review_date` datetime DEFAULT NULL,
  PRIMARY KEY (`sno`)
)
ALTER TABLE `user_reviews` ADD COLUMN `google_lonk` TEXT NULL AFTER `review_date`; 
ALTER TABLE `user_reviews` CHANGE `google_lonk` `google_link` TEXT CHARSET latin1 COLLATE latin1_swedish_ci NULL; 
*/
require_once("simple_html_dom.php");
function getDescription($url,$page='')
{
	if($page==''){
		$fields = array(
			'id'			=>	'com.hangover.familysafety',
		);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		return curl_exec($ch);
	}else{
		$fields = array(
			'id'				=>	'com.hangover.familysafety',
			'pageNum'			=>	$page,
			'reviewSortOrder'	=>	0,
			'reviewType'		=>	0,
		);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		return curl_exec($ch);
	}
}
	$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db('family_safety');
	
	$desurll="https://play.google.com/store/apps/details";
	$description=getDescription($desurll);
	$dommm = new simple_html_dom();
	$dommm->load($description);
	$totalReviewss=$dommm->find('div[class="stars-count"]');
	if(count($totalReviewss)!=0){
		foreach($totalReviewss as $totallreviews){
			$reviewsTotall=str_replace('(','',$totallreviews->outertext);
			$reviewsTotal=str_replace(')','',$reviewsTotall);
		}
	}else{
		$reviewsTotal="";
	}
	//echo "<pre>";print_r($reviewsTotal);exit;
	$sitereviewsTotal=(int)strip_tags($reviewsTotal);
	$sql = 'SELECT * FROM user_reviews';
	$page=mysql_query( $sql, $conn );
	$totalReviews=mysql_num_rows($page);
	$loadReviewRemaing=$sitereviewsTotal-$totalReviews;
	$remaingPages=ceil($loadReviewRemaing/20);
	$remainder=$loadReviewRemaing%20;
	for($i='0';$i<$remaingPages;$i++){
		$desurl = 'https://play.google.com/store/getreviews';
		$result=getDescription($desurl,$i);
		$items = explode('\u003cdiv class\u003d\"single-review\"\u003e',$result);
		foreach($items as $key=>$item) {
			if($remaingPages==($i+1) && $remainder==$key){
				break;
			}
			preg_match('/author-image(.*?) avatar image/',$item,$matches);
			$author = explode('alt\u003d\"',$matches['1']);
			if(trim(addslashes($author['1']))!=""){
			
				$author ='"'.trim(addslashes($author['1'])).'"';
				preg_match('/author-image(.*?)div/',$item,$matches);
				$authorImage = explode('src\u003d\"',$matches['1']);
				$authorImage = $authorImage['1'];
				$authorImage = str_replace('\"\u003e \u003c/a\u003e   \u003c','',$authorImage);
				$authorImage = '"'.str_replace('u003d','=',$authorImage).'"';
				
				$googlelink=explode('\u003cimg class\u003d\"author-image\"',$item);
				$googlelink=$googlelink[0];
				$googlelink=str_replace('\u003ca href\u003d\"','',$googlelink);
				$googlelink=str_replace('\"\u003e ','',$googlelink);
				$googlelink=str_replace('003d','',$googlelink);
				$googlelink=str_replace(' ','','"https://play.google.com'.str_replace('\\u','=',$googlelink).'"');
				
				preg_match('/review-date(.*?)span/',$item,$matches);
				$review_date = $matches['1'];
				$review_date = str_replace('\\"\\u003e', '', $review_date);
				$review_date = str_replace('\\\\u003cdiv class\\\\u003d\\\\\\"', '', $review_date);
				$review_date = str_replace('\\u003c/', '', $review_date);
				//echo $review_date;exit;
				preg_match('/review-title(.*?)review-link/',$item,$matches);
				$doc_review_content = addslashes($matches['1']);
				$doc_review_content = explode('span', $doc_review_content);
				$doc_review_content['0'] = str_replace('\\\\\\"\\\\u003e', '', $doc_review_content['0']);
				$doc_review_content['0'] = str_replace('\\\\u003c/', '', $doc_review_content['0']);
				$doc_review_title = trim($doc_review_content['0']);
				$doc_review_content['1'] = str_replace('\\\\u003e', '', $doc_review_content['1']);
				$doc_review_content['1'] = str_replace('\\\\u003cdiv class\\\\u003d\\\\\\"', '', $doc_review_content['1']);
				$doc_review_text = '"'.trim($doc_review_content['1']).'"';
				
				preg_match('/current-rating(.*?)%/',$item,$matches);
				$doc_review_rating = addslashes($matches['1']);
				$doc_review_rating = str_replace('\\\\\\"review_date style\\\\u003d\\\\\\"width: ', '', $doc_review_rating);
				$doc_review_rating = ($doc_review_rating / 20);  
				$rating = '"'.trim(number_format($doc_review_rating, 1, '.', '')).'"';
				
				if($author !='') {
					$review_date = strtotime($review_date);
					$review_date = '"'.date('Y-m-d H:i:s', $review_date).'"';
				}
				//echo $review_date;exit;
				/*
				$information[$author]['image']=$authorImage;
				$information[$author]['review']=$doc_review_text;
				$information[$author]['date']=$review_date;
				$information[$author]['rating']=$rating;
				*/
				$sql = 'INSERT INTO user_reviews (user_name,image,review,rating,review_date,google_link) VALUES ('.$author.','.$authorImage.','.$doc_review_text.','.$rating.','.$review_date.','.$googlelink.')';
				
				$result=mysql_query( $sql, $conn );
			}
		}
	}
	//echo count($information).'<br/>';
	//echo '<pre>'; print_r($information);
	echo "Store data successfully\n";
	
?>
